package com.ebao.service.talk.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.ebao.constants.UserType;
import com.ebao.dao.base.IBaseJpaDao;
import com.ebao.po.doctor.TDoctor;
import com.ebao.po.talk.TTalk;
import com.ebao.po.talk.TTalkPush;
import com.ebao.po.user.TUser;
import com.ebao.service.base.impl.CrudServiceImpl;
import com.ebao.service.talk.ITalkPushService;
import com.ebao.service.user.IFollowService;

@Service("talkPushService")
public class TalkPushServiceImpl extends CrudServiceImpl<TTalkPush> implements ITalkPushService {

	@Resource(name="talkPushDao")
	public void setDao(IBaseJpaDao<TTalkPush> dao) {  
		super.setDao(dao);
	}
	
	@Resource
	private IFollowService followService;
	
	/**
	 * 用户推送说说
	 * 
	 * @param user 说说发送者
	 * @param talk 要发送的说说
	 */
	public void userPush(TUser user, TTalk talk){
		List<TUser> followUserList = followService.getFollowUsers(user);
		List<TDoctor> followDoctorList = followService.getFollowDoctors(user);
		
		List<TTalkPush> talkPushList = new ArrayList<TTalkPush>();
		
		// 循环插入TTalkPush中,推说说
		if(CollectionUtils.isNotEmpty(followUserList)){
			for(TUser u : followUserList) {
				TTalkPush talkPush = new TTalkPush();
				talkPush.setTalk(talk);
				talkPush.setReceiveType(UserType.COMMON_USER);
				talkPush.setReceiverId(u.getId());
				talkPush.setPushDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				talkPush.setStatus(0);
				talkPushList.add(talkPush);
			}
		}
		
		if(CollectionUtils.isNotEmpty(followDoctorList)){
			for(TDoctor d : followDoctorList) {
				TTalkPush talkPush = new TTalkPush();
				talkPush.setTalk(talk);
				talkPush.setReceiveType(UserType.DOCTOR);
				talkPush.setReceiverId(d.getId());
				talkPush.setPushDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				talkPush.setStatus(0);
				talkPushList.add(talkPush);
			}
		}
		
		// 用户自己也可以查看
		TTalkPush tp = new TTalkPush();
		tp.setTalk(talk);
		tp.setReceiveType(UserType.COMMON_USER);
		tp.setReceiverId(user.getId());
		tp.setPushDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		tp.setStatus(0);
		talkPushList.add(tp);
				
		// 保存
		if(CollectionUtils.isNotEmpty(talkPushList)){
			this.save(talkPushList);
		}
		
	}
	
	/**
	 * 医生推送说说
	 * 
	 * @param doctor 说说发送者
	 * @param talk   要发送的说说
	 */
	public void doctorPush(TDoctor doctor, TTalk talk){
		List<TUser> followUserList = followService.getFollowUsers(doctor);
		List<TDoctor> followDoctorList = followService.getFollowDoctors(doctor);
		
		List<TTalkPush> talkPushList = new ArrayList<TTalkPush>();
		
		// 循环插入TTalkPush中,推说说
		if(CollectionUtils.isNotEmpty(followUserList)){
			for(TUser u : followUserList) {
				TTalkPush talkPush = new TTalkPush();
				talkPush.setTalk(talk);
				talkPush.setReceiveType(UserType.COMMON_USER);
				talkPush.setReceiverId(u.getId());
				talkPush.setPushDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				talkPush.setStatus(0);
				talkPushList.add(talkPush);
			}
		}
		
		if(CollectionUtils.isNotEmpty(followDoctorList)){
			for(TDoctor d : followDoctorList) {
				TTalkPush talkPush = new TTalkPush();
				talkPush.setTalk(talk);
				talkPush.setReceiveType(UserType.DOCTOR);
				talkPush.setReceiverId(d.getId());
				talkPush.setPushDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
				talkPush.setStatus(0);
				talkPushList.add(talkPush);
			}
		}
		
		// 医生自己也可以查看
		TTalkPush tp = new TTalkPush();
		tp.setTalk(talk);
		tp.setReceiveType(UserType.DOCTOR);
		tp.setReceiverId(doctor.getId());
		tp.setPushDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		tp.setStatus(0);
		talkPushList.add(tp);
		
		// 保存
		if(CollectionUtils.isNotEmpty(talkPushList)){
			this.save(talkPushList);
		}
		
	}

}
